package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import db.parking.beans.Orders;
import db.parking.beans.Subscriber;

import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.forms.CheckInSubscriberForm;
import vcp.gui.forms.CheckOutForm;
import vcp.models.CheckOutModel;
import vcp.views.CheckOutView;

/**
 * Check out controller 
 * @author G15
 * 
 */
public class CheckOutController extends AbstractController implements
ActionListener, IController {
    CheckOutModel  checkOutModel;
    CheckOutView checkOutView;
	
	/* (non-Javadoc)
	 * @see vcp.common.IController#addModel(java.lang.Object)
	 */
	@Override
	public void addModel(Object model) {
		this.checkOutModel = (CheckOutModel)model;	
	}

	/* (non-Javadoc)
	 * @see vcp.common.IController#addView(java.lang.Object)
	 */
	@Override
	public void addView(Object view) {
		this.checkOutView = (CheckOutView)view;	
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {

		boolean isValidForm=false;
		Orders bean = new Orders();
		
		CheckOutForm form = checkOutView.getForm();
		
        if(form.getTxtCID().getText().isEmpty() ||form.getTxtCID().getText().isEmpty() )
        {
        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		    isValidForm = false;
		    return;
        }
        
        //Parse all data	
		String cusID = form.getTxtCID().getText();
		String carNo = form.getTxtCARID().getText();
		
		
		//Input Validity Check
		if (cusID.equals("") || carNo.equals("") || Integer.parseInt(cusID) < 0 ||Integer.parseInt(carNo)<0) {
			JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
			isValidForm = false;
		}

	   else{
			isValidForm = true;
		}
		
		if(isValidForm==true){
			 try {
				bean.setUid(cusID);
				bean.setCarNo(carNo);
				this.checkOutModel.doCheckout(bean);		
				} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	    }
     }
	
}
